/**
 * Created by septimus on 15/1/3.
 */

function regController($scope, $http) {
    $scope.error = false;
    $scope.incomplete = true;

    $scope.$watch('isAgree', function() {$scope.validate();});

    $scope.validate = function() {
        if ($scope.isAgree) {
            $scope.incomplete = false;
        } else {
            $scope.incomplete = true;
        }
    };

    $scope.submit = function() {
        var url = application.getUrlRegister(),
            data = {
                user_name: $scope.userName,
                passwd: $scope.psw,
                sex: $scope.sex,
                confirm_passwd: $scope.pswRepeat,
                phone: $scope.phone
            },
            transFn = function(data) {
                return $.param(data);
            },
            postCfg = {
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                transformRequest: transFn
            };

        $http.post(url, data, postCfg)
            .success(function(data) {
                if (data.status == 1) {
                    window.location.href = application.getUrlPublish();
                } else {
                    alert(data);
                }
        });
    };
}

function loginController($scope, $http) {
    $scope.error = false;
    $scope.incomplete = true;

    $scope.$watch('phone', function() {$scope.validate();});
    $scope.$watch('psw', function() {$scope.validate();});

    $scope.validate = function() {
        if ($scope.isAgree) {
            $scope.incomplete = false;
        } else {
            $scope.incomplete = true;
        }
    };

    $scope.submit = function() {
        var url = application.getUrlLogin(),
            data = {
                phone: $scope.phone,
                passwd: $scope.psw
            },
            transFn = function(data) {
                return $.param(data);
            },
            postCfg = {
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                transformRequest: transFn
            };

        $http.post(url, data, postCfg)
            .success(function(data) {
                if (data.status == 1) {
                    window.location.href = application.getUrlPublish();
                }
            });
    }
}

// 'I will publish mission' index
function publishIndexController($scope, $http) {
    $scope.userName = '';

    $scope.jobs = [
        {
            id: 1,
            name: '导演(团队-副导演)',
            active: false
        },
        {
            id: 2,
            name: '武术',
            active: false
        },
        {
            id: 3,
            name: '其他',
            active: false
        },
        {
            id: 4,
            name: '编剧',
            active: false
        }
    ];
    $scope.durations = [
        {
            id: 1,
            name: '三天内',
            active: false
        },
        {
            id: 2,
            name: '双周内',
            active: false
        },
        {
            id: 3,
            name: '三个月内',
            active: false
        },
        {
            id: 4,
            name: '长期',
            active: false
        }
    ];

    $scope.jobs[0].active = $scope.durations[0].active = true;

    $scope.jobClick = function(index) {
        $scope.jobs[index].active = !$scope.jobs[index].active;
        $scope.search();
    };

    $scope.durationClick = function(index) {
        $scope.durations[index].active = !$scope.durations[index].active;
        $scope.search();
    };

    // Search information of the second part with conditions.
    $scope.search = function() {
        var jobs = getActiveTag($scope.jobs, 'name'),
            durations = getActiveTag($scope.durations, 'id');

        var url = application.getUrlGetSecond() + '/jobs/' + jobs + '/duration/' + durations;
        $http.get(url).success(function(data) {
//            alert(JSON.stringify(data));
            if(data.status == 1) {
                $scope.secondPart = data.url;
            } else {
                $scope.secondPart = '';
            }
        });
    };

    $scope.search();
}

// 'Publish mission now' page
function publishSubmitController($scope, $http) {
    $scope.jobs = [
        {
            id: 1,
            name: '导演(团队-副导演)',
            active: false
        },
        {
            id: 2,
            name: '武术',
            active: false
        },
        {
            id: 3,
            name: '其他',
            active: false
        },
        {
            id: 4,
            name: '编剧',
            active: false
        }
    ];
    $scope.durations = [
        {
            id: 1,
            name: '三天内',
            active: false
        },
        {
            id: 2,
            name: '双周内',
            active: false
        },
        {
            id: 3,
            name: '三个月内',
            active: false
        },
        {
            id: 4,
            name: '长期',
            active: false
        }
    ];

    $scope.jobs[0].active = $scope.durations[0].active = true;

    $scope.jobClick = function(index) {
        $scope.jobs[index].active = !$scope.jobs[index].active;
    };

    $scope.durationClick = function(index) {
        $scope.durations[index].active = !$scope.durations[index].active;
    };

    $scope.submit = function() {
        var jobs = getActiveTag($scope.jobs, 'name'),
            durations = getActiveTag($scope.durations, 'id'),
            url = application.getUrlPublishMission(),
            data = {
                mission_name: $scope.missionName,
                jobs: jobs,
                duration: durations,
                salary1: $scope.salary1,
                salary2: $scope.salary2,
                location1: $scope.location1,
                location2: $scope.location2,
                description: $scope.description,
                valid_time: $scope.validTime,
                isagent: $scope.isAgent

            },
                transFn = function(data) {
                    return $.param(data);
                },
                postCfg = {
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: transFn
            };

        $http.post(url, data, postCfg)
            .success(function(data) {
                if(data.status == 1) {
                    alert("发布成功！");
                    window.location.href = application.getUrlPublish();
                } else {
                    alert(JSON.stringify(data));
                }
            });
    };
}

// 'I will fetch mission' index
function fetchIndexController($scope, $http) {
    $scope.userName = '';

    $scope.jobs = [
        {
            id: 1,
            name: '导演(团队-副导演)',
            active: false
        },
        {
            id: 2,
            name: '武术',
            active: false
        },
        {
            id: 3,
            name: '其他',
            active: false
        },
        {
            id: 4,
            name: '编剧',
            active: false
        }
    ];
    $scope.durations = [
        {
            id: 1,
            name: '三天内',
            active: false
        },
        {
            id: 2,
            name: '双周内',
            active: false
        },
        {
            id: 3,
            name: '三个月内',
            active: false
        },
        {
            id: 4,
            name: '长期',
            active: false
        }
    ];

    $scope.jobs[0].active = $scope.durations[0].active = true;

    $scope.jobClick = function(index) {
        $scope.jobs[index].active = !$scope.jobs[index].active;
        $scope.search();
    };

    $scope.durationClick = function(index) {
        $scope.durations[index].active = !$scope.durations[index].active;
        $scope.search();
    };

    // Search information of the second part with conditions.
    $scope.search = function() {
        var jobs = getActiveTag($scope.jobs, 'name'),
            durations = getActiveTag($scope.durations, 'id');

        var url = application.getUrlFetchMission() + '/jobs/' + jobs + '/duration/' + durations;
        $http.get(url).success(function(data) {
//            alert(JSON.stringify(data));
            if(data.status == 1) {
                $scope.missions = data.url;
            } else {
                $scope.missions = '';
            }
        });
    };

    $scope.search();
}

// Navigation bar
function navController($scope, $location) {
    $scope.navItems = [
        {
            title: 'getmission',
            text: '我要接活',
            location: '/publisher',
            href: application.getUrlPublish()
        },
        {
            title: 'resume',
            text: '简历',
            location: '/resume',
            href: application.getUrlPublish()
        },
        {
            title: 'msg',
            text: '消息',
            location: '/msg',
            href: application.getUrlPublish()
        },
        {
            title: 'profile',
            text: '个人',
            location: '/profile',
            href: application.getUrlPublish()
        }
    ];
//    $scope.selectedItem = 'getmission';
    $scope.itemClick = function(itemTitle) {
        $scope.selectedItem = itemTitle;
    };

    //Initialization
    var curLocation = $location.absUrl();
    for (var i = 0, len = $scope.navItems.length; i < len; i++) {
        var navItem = $scope.navItems[i];
        if(curLocation == navItem.href) {
            $scope.selectedItem = navItem.title;
        }
    }
}

// Tag list utils
function getActiveTag(arr, field) {
    var result = '';
    var tempArr = new Array();
    for (var i = 0; i < arr.length; i++) {
        if(arr[i].active) {
            tempArr.push(arr[i][field]);
        }
    }
    result = tempArr.join(',');
    return result;
}